package com.xmim.server.examples;

import com.xmim.fulcrum.FulcrumFactory;
import com.xmim.server.serialization.ProtobufDecoder;
import com.xmim.server.serialization.ProtobufEncoder;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;

import java.util.Map;

public class NettyChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final FulcrumFactory factory;
    private final Map<String, ChannelHandler> handlers;

    public NettyChannelInitializer(FulcrumFactory factory, Map<String, ChannelHandler> handlers) {
        this.factory = factory;
        this.handlers = handlers;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ch.pipeline().addLast("ProtobufDecoder", new ProtobufDecoder(factory.getCodec()));
        ch.pipeline().addLast("ProtobufEncoder", new ProtobufEncoder(factory.getCodec()));
        for (Map.Entry<String, ChannelHandler> entry : handlers.entrySet()) {
            ch.pipeline().addLast(entry.getKey(), entry.getValue());
        }
    }
}
